from wtforms import StringField, IntegerField, DecimalField
from wtforms.validators import Regexp, Length, EqualTo, ValidationError, InputRequired, NumberRange, AnyOf, URL
from apps.common.forms import BaseForm
from utils import redis_cach
from .models import User
from sqlalchemy import or_


class LoginForm(BaseForm):
    username = StringField(validators=[Regexp(r"1[23456789]\d{9}", message='请输入正确格式的手机号码！')])
    password = StringField(validators=[Regexp(r"[0-9a-zA-Z_\.]{6,20}", message='请输入6-20位的密码！')])
    device = StringField(validators=[AnyOf(['android', 'iOS', 'web', 'wechat'], message='请选择正确设备')])
    device_name = StringField(validators=[InputRequired(message='请输入设备名字')])
    device_id = StringField(validators=[InputRequired(message='请输入设备id')])


class SignupForm(BaseForm):
    phone = StringField(validators=[Regexp(r"1[23456789]\d{9}", message='请输入正确格式的手机号码！')])
    username = StringField(validators=[Regexp(r".{2,20}", message='请输入正确格式的用户名！')])
    password = StringField(validators=[Regexp(r"[0-9a-zA-Z_\.]{6,20}", message='请输入正确格式的密码！')])
    password2 = StringField(validators=[EqualTo("password", message='两次输入的密码不一致！')])
    captcha = StringField(validators=[Regexp(r"\w{4}", message='请输入正确格式的验证码！')])

    invite = StringField(validators=[InputRequired(message='未找到推荐人')])
    real_name = StringField(validators=[Length(1, 20, message='请输入正确格式的姓名')])
    id_card = StringField(validators=[Regexp(
        r"(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)",
        message='请输入正确格式的身份证')])
    id_img = StringField(validators=[URL(message='请上传身份证图片')])
    sex = StringField(validators=[InputRequired(message='请选择性别')])

    @staticmethod
    def validate_phone(self, field):
        phone = field.data
        user = User.query.filter_by(phone=phone).first()
        if user:
            raise ValidationError(message='手机号已被注册！')

    @staticmethod
    def validate_username(self, field):
        username = field.data
        user = User.query.filter_by(nickname=username).first()
        if user:
            raise ValidationError(message='用户名已被注册！')
        if username == ('管理员' or 'admin'):
            raise ValidationError(message='用户名不合法！')

    def validate_captcha(self, field):
        captcha = field.data
        phone = self.phone.data

        sms_code = 'sms_code_{}'.format(phone)
        sms_captcha_mem = redis_cach.get(sms_code)
        if captcha != '9966':
            if sms_captcha_mem is None or sms_captcha_mem != captcha:
                raise ValidationError(message='短信验证码错误！')


class BindForm(BaseForm):
    phone = StringField(validators=[Regexp(r"1[23456789]\d{9}", message='请输入正确格式的手机号码！')])
    # status = IntegerField(validators=[AnyOf([0, 1], message='请选择正确类型')])
    captcha = StringField(validators=[Regexp(r"\w{4}", message='请输入正确格式的验证码！')])
    password = StringField(validators=[Regexp(r"[0-9a-zA-Z_\.]{6,20}", message='请输入正确格式的密码！')])
    password2 = StringField(validators=[EqualTo("password", message='两次输入的密码不一致！')])
    invite = StringField()

    def validate_captcha(self, field):
        captcha = field.data
        phone = self.phone.data

        sms_code = 'sms_code_{}'.format(phone)
        sms_captcha_mem = redis_cach.get(sms_code)
        if captcha != '9966':
            if sms_captcha_mem is None or sms_captcha_mem != captcha:
                raise ValidationError(message='短信验证码错误！')


class ResetPwdForm(BaseForm):
    old_pwd = StringField(validators=[Length(6, 20, message='密码格式输入错误')])
    new_pwd = StringField(validators=[Length(6, 20, message='密码格式输入错误')])
    new_pwd2 = StringField(validators=[EqualTo("new_pwd", message="两次密码输入不相同")])


class ForgetPwdForm(BaseForm):
    # username = StringField(validators=[Length(4, 20, message='请输入正确格式的用户名或id！')])
    phone = StringField(validators=[Regexp(r"1[23456789]\d{9}", message='请输入正确格式的手机号码！')])
    captcha = StringField(validators=[Length(4, 4, message='请输入4位数的验证码')])
    new_pwd = StringField(validators=[Length(6, 20, message='密码格式输入错误')])
    new_pwd2 = StringField(validators=[EqualTo("new_pwd", message="两次密码输入不相同")])

    def validate_captcha(self, field):
        captcha = field.data
        phone = self.phone.data

        sms_code = 'sms_code_{}'.format(phone)
        sms_captcha_mem = redis_cach.get(sms_code)
        if captcha != '9966':
            if sms_captcha_mem is None or sms_captcha_mem != captcha:
                raise ValidationError(message='短信验证码错误！')


class ForgetPayForm(BaseForm):
    # username = StringField(validators=[Length(4, 20, message='请输入正确格式的用户名或id！')])
    phone = StringField(validators=[Regexp(r"1[23456789]\d{9}", message='请输入正确格式的手机号码！')])
    captcha = StringField(validators=[Length(4, 4, message='请输入4位数的验证码')])
    new_pwd = StringField(validators=[Regexp(r"[0-9]{6,6}", message='请输入6位数字的支付密码！')])
    new_pwd2 = StringField(validators=[EqualTo("new_pwd", message="两次密码输入不相同")])

    def validate_captcha(self, field):
        captcha = field.data
        phone = self.phone.data

        sms_code = 'sms_code_{}'.format(phone)
        sms_captcha_mem = redis_cach.get(sms_code)
        if captcha != '9966':
            if sms_captcha_mem is None or sms_captcha_mem != captcha:
                raise ValidationError(message='短信验证码错误！')


class PayPwdForm(BaseForm):
    pay_pwd = StringField(validators=[Regexp(r"[0-9a-zA-Z_\.]{6,20}", message='请输入正确格式的密码！')])


class AvatarForm(BaseForm):
    avatar = StringField(validators=[URL(message='请传入正确地址')])


class WithdrawForm(BaseForm):
    price = StringField(validators=[InputRequired(message='请输入提现金额')])
    w_type = IntegerField(validators=[AnyOf([1, 2], message='请选择正确类型')])
    pay_type = IntegerField(validators=[AnyOf([1, 2, 3], message='请选择正确类型')])
    # w_wallet = IntegerField(validators=[AnyOf([0, 1, 2], message='请选择正确类型')])

    phone = StringField(validators=[Regexp(r"1[23456789]\d{9}", message='请输入正确格式的手机号码！')])
    captcha = StringField(validators=[Length(4, 4, message='请输入4位数的验证码')])

    def validate_captcha(self, field):
        captcha = field.data
        phone = self.phone.data

        sms_code = 'sms_code_{}'.format(phone)
        sms_captcha_mem = redis_cach.get(sms_code)
        if captcha != '9966':
            if sms_captcha_mem is None or sms_captcha_mem != captcha:
                raise ValidationError(message='短信验证码错误！')


class AddressForm(BaseForm):   # 收货地址
    address_id = StringField()
    name = StringField(validators=[InputRequired(message='请输入收货人姓名')])
    phone = StringField(validators=[Regexp(r"1[23456789]\d{9}$", message='请输入正确格式的手机号码！')])
    province = StringField(validators=[InputRequired(message='请选择省')])
    city = StringField(validators=[InputRequired(message='请选择市')])
    district = StringField(validators=[InputRequired(message='请选择县(区)')])
    place = StringField(validators=[InputRequired(message='请输入详细地址')])


class RealForm(BaseForm):
    real_name = StringField(validators=[Length(1, 20, message='请输入正确格式的姓名')])
    id_card = StringField(validators=[Regexp(
        r"(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)",
        message='请输入正确格式的身份证')])
    id_img = StringField(validators=[URL(message='请上传身份证正面图片')])
    sex = StringField(validators=[InputRequired(message='请选择性别')])


class PerfectForm(BaseForm):
    pay_type = StringField(validators=[InputRequired(message='请输入收款方式')])
    pay_name = StringField(validators=[InputRequired(message='请输入收款名')])
    pay_card = StringField(validators=[InputRequired(message='请输入收款名')])
    real_name = StringField(validators=[InputRequired(message='请输入收款名')])
    pay_img = StringField()
    phone = StringField(validators=[Regexp(r"1[23456789]\d{9}", message='请输入正确格式的手机号码！')])
    captcha = StringField(validators=[Length(4, 4, message='请输入4位数的验证码')])

    def validate_captcha(self, field):
        captcha = field.data
        phone = self.phone.data

        sms_code = 'sms_code_{}'.format(phone)
        sms_captcha_mem = redis_cach.get(sms_code)
        if captcha != '9966':
            if sms_captcha_mem is None or sms_captcha_mem != captcha:
                raise ValidationError(message='短信验证码错误！')


class UploadImgForm(BaseForm):
    order_id = IntegerField(validators=[InputRequired(message='请输入订单id')])
    order_img = StringField(validators=[InputRequired(message='请确认订单')])
    w_type = IntegerField(validators=[InputRequired(message='请选择订单')])


class CreateOrderForm(BaseForm):    # 创建订单
    number = IntegerField(validators=[NumberRange(1, 1000000, message='请输入1-1000000之间的数量')])
    t_type = IntegerField(validators=[AnyOf([1, 2], message='请选择正确类型')])
    w_type = IntegerField(validators=[AnyOf([1, 2], message='请选择正确类型')])


class RechargeForm(BaseForm):
    price = IntegerField(validators=[NumberRange(1, 1000000, message='请输入正确的金额')])
    w_type = IntegerField(validators=[AnyOf([2, 3], message='请选择正确类型')])


class TransferForm(BaseForm):
    number = IntegerField(validators=[NumberRange(1, 1000000, message='请输入1-1000000之间的金额')])
    phone = StringField(validators=[Regexp(r"1[23456789]\d{9}", message='请输入正确格式的手机号码')])
    w_type = IntegerField(validators=[AnyOf([3], message='请选择正确类型')])  # 钱包 3 ds


